package cn.ctodb.openapi.idcard.service;

import cn.ctodb.openapi.idcard.service.dto.DataIdcardDTO;
import cn.ctodb.openapi.idcard.service.dto.IdcardDTO;
import org.apache.commons.lang.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by cc on 2017/3/31.
 */
@Service
public class IdcardService {

    @Resource
    private DataIdcardService dataIdcardService;

    /**
     * 身份信息认证
     *
     * @param name
     * @param cardno
     */
    public IdcardDTO idc(String name, String cardno) {
        IdcardDTO idcardDTO = new IdcardDTO();
        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(cardno)) {
            idcardDTO.setCode("010");
            idcardDTO.setMsg("姓名与身份证号不能为空.");
            return idcardDTO;
        }
        cardno = cardno.trim();
        name = name.trim();
        try {
            DataIdcardDTO nciic = dataIdcardService.findOneByNameAndCardno(name, cardno);
            if (nciic == null) {
                String result = ApixHttp.getapi(ApixHttp.IDCHECK_URL + "?type=idcard&cardno=" + cardno + "&name=" + name,
                    ApixHttp.IDCHECK_APIKEY);
                nciic = exec(result);
            }
            if (nciic == null) {
                idcardDTO.setCode("010");
                idcardDTO.setMsg("库无");
            } else {
                idcardDTO.setAddress(nciic.getAddress());
                idcardDTO.setBirthday(nciic.getBirthday());
                idcardDTO.setCardno(nciic.getCardno());
                idcardDTO.setName(nciic.getName());
                idcardDTO.setSex(nciic.getSex());
                idcardDTO.setMsg("成功");
                idcardDTO.setCode("0");
            }
        } catch (Exception e) {
            idcardDTO.setCode("010");
            idcardDTO.setMsg("严重错误：" + e.getMessage());
        }
        return idcardDTO;
    }

    public DataIdcardDTO exec(String str) {
        if (str == null) return null;
        System.out.println(str);
        JSONTokener jsonTokener = new JSONTokener(str);
        JSONObject json = null;
        try {
            DataIdcardDTO dataIdcardDTO = new DataIdcardDTO();
            json = (JSONObject) jsonTokener.nextValue();
            if (json.getInt("code") == 0) {
                json = json.getJSONObject("data");
                dataIdcardDTO.setBirthday(json.getString("birthday"));
                dataIdcardDTO.setAddress(json.getString("address"));
                dataIdcardDTO.setCardno(json.getString("cardno"));
                dataIdcardDTO.setName(json.getString("name"));
                if ("M".equals(json.getString("sex"))) {
                    dataIdcardDTO.setSex("男");
                } else {
                    dataIdcardDTO.setSex("女");
                }
                dataIdcardService.save(dataIdcardDTO);
                return dataIdcardDTO;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}
